import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QGridLayout
from PyQt5.QtGui import QPixmap, QFont
from PyQt5.QtCore import QTimer, QDateTime, Qt


class FloatingLabelDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("Floating Label Example")
        #self.setGeometry(300, 200, 800, 200)
        self.setFixedSize(1500, 300) 
        # 创建一个主网格布局
        grid_layout = QGridLayout(self)
        grid_layout.setContentsMargins(0, 0, 0, 0)
        grid_layout.setSpacing(0)

        # 背景图片 Label
        label_bg = QLabel(self)
        label_bg.setPixmap(QPixmap('views/assets/header_bg.png').scaled(self.width(), 200, Qt.KeepAspectRatioByExpanding))
        
        #label_bg.setPixmap(QPixmap("views/assets/header_bg.png").scaled(800, 200, Qt.KeepAspectRatioByExpanding))
        label_bg.setAlignment(Qt.AlignTop | Qt.AlignHCenter)
        grid_layout.addWidget(label_bg, 0, 0)  # 放置背景图片

        # 浮动时间 Label
        label_time = QLabel(self)
        label_time.setFont(QFont("Arial", 14))
        label_time.setStyleSheet("""
            color: white; 
            background-color: rgba(0, 0, 0, 0.6); 
            padding: 5px; 
            border-radius: 5px;
        """)
        label_time.setFixedSize(200, 30)  # 固定大小，控制浮动效果
        label_time.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        grid_layout.addWidget(label_time, 0, 0, Qt.AlignTop | Qt.AlignLeft)  # 浮动显示时间标签

        # 每秒更新时间
        self.update_time(label_time)
        timer = QTimer(self)
        timer.timeout.connect(lambda: self.update_time(label_time))
        timer.start(1000)

        # 设置主布局为网格布局
        self.setLayout(grid_layout)

    def update_time(self, label):
        """更新时间"""
        current_time = QDateTime.currentDateTime()
        time_str = current_time.toString("yyyy-MM-dd HH:mm:ss")
        label.setText(time_str)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FloatingLabelDemo()
    window.show()
    sys.exit(app.exec_())
